package org.mil.gui;


import java.awt.Component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.mil.logic.MediaInfoCollector;

public class MainPanel extends JPanel {

	private static final long serialVersionUID = -251L;
	public MainForm father;

	private JTextField urlTextField = new JTextField(26);
	String[] types = {  "HTML格式", "TXT格式"};
	ButtonGroup radioGroup = new ButtonGroup();
	JLabel logo=new JLabel(MainForm.LINK);
	JButton buttonBrowse = new JButton("指定媒体库目录");
	JButton buttonExport = new JButton("导出媒体信息报表");
	JCheckBox chkSubDir = new JCheckBox("连同子目录");
	 ProgressMonitor monitor=new ProgressMonitor(null, "信息收集进度","信息收集进度",0,233);
		
	public MainPanel() {

		buttonBrowse.addActionListener(new SpecifyDirAction());
		buttonExport.addActionListener(new ExportAction());
		chkSubDir.setMnemonic(KeyEvent.VK_C);
		chkSubDir.setSelected(true);
		
		// 输出格式选择框

		Box typeSelectBox = Box.createVerticalBox();

		for (String type : types) {
			JRadioButton b = new JRadioButton(type);
			b.setSelected(true);
			radioGroup.add(b);
			typeSelectBox.add(b);
		}
		
		typeSelectBox.setBorder(BorderFactory.createTitledBorder("输出格式"));

		Box chkOptBox = Box.createVerticalBox();
		chkOptBox.add(new JCheckBox("扫描子目录"));
		chkOptBox.add(buttonBrowse);

		// 设定布局
		Box row1 = Box.createHorizontalBox();
		Box row2 = Box.createHorizontalBox();
		Box row3 = Box.createHorizontalBox();

		row1.add(urlTextField);
		row1.add(buttonBrowse);

		row2.add(typeSelectBox);
		row2.add(chkOptBox);
		row3.add(logo);
		row3.add(Box.createGlue());
		row3.add(buttonExport);

		Box col = Box.createVerticalBox();
		col.add(row1);
		col.add(row2);
		col.add(row3);
		this.add(col);

	}

	class SpecifyDirAction implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {

			JFileChooser chooser = new JFileChooser();
			// chooser.setCurrentDirectory(new java.io.File("."));
			chooser.setDialogTitle("指定所要扫描的目录");
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			chooser.setAcceptAllFileFilterUsed(false);

			if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 			
				urlTextField.setText(chooser.getSelectedFile()
						.getAbsolutePath());
			
			System.out.println("SelectedFile:"+ chooser.getSelectedFile());
		}
	}

	class ExportAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String radio = SwingUtil.getSelectionText(radioGroup);
			final Component parent = (Component) arg0.getSource();
			FileNameExtensionFilter filter = null;
			
			String type = "html";
			if (radio!=null){
				if (radio.equals(types[0])){
					type = "html";
					filter= new FileNameExtensionFilter("网页(HTML)", "html", "xhtml");
				}					
				if (radio.equals(types[1])){
					type = "txt";
					filter= new FileNameExtensionFilter("文本文件", "txt");
				}
					
			}
			boolean containSubdir = chkSubDir.isSelected();
			String srcUrl = urlTextField.getText();
			String tarUrl = null;

			JFileChooser chooser = new JFileChooser();
			chooser.setDialogTitle("指定保存路径");
			chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			chooser.setFileFilter(filter);			
			chooser.setApproveButtonText("导出");

			if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
				tarUrl = chooser.getSelectedFile().getAbsolutePath();
				// 选定保存路径后，开始正式导出报表
				
				final MediaInfoCollector mic = new MediaInfoCollector();
				mic.loadFiles(srcUrl, containSubdir);	
				mic.configReport(tarUrl, type);
				if (mic.getTotalFiles()>0){
					mic.addCollectionProgressListener( 
							new CollectProgressMonitor(parent,0, mic.getTotalFiles()));
					
				
				new Thread(){ 
			       public void run(){ 
					mic.collect();
					int x=mic.exportReport();
					if (x>=0)
						JOptionPane.showMessageDialog(null, "导出数据成功！ ");
					else 
						JOptionPane.showMessageDialog(null, "导出数据时发生错误，操作失败。 ");
			       } 
			    }.start(); 
				
			}else  
				JOptionPane.showMessageDialog(null, "未指定媒体库目录 ");
				
		               
			}
		}
	}

}
